﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Framework.WebSite
{
    public partial class TestTheme : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CMSApproveService service = new CMSApproveService();

            service.Excute(new CMSActionContext() {  Canceled=false,  IsValid=true, ActionName="Add"});
        }
    }

    public class CMSApproveService 
    {
        #region Excute Action

        public void Excute( CMSActionContext context)
        {
            List<ActionHandler> handlers = new List<ActionHandler>(); // Get according to the request content type and action type 
            handlers.Add(new ActionHandler("1"));
            handlers.Add(new ActionHandler("2"));
            handlers.Add(new ActionHandler("3"));
            handlers.Add(new ActionHandler("4"));
            var enumerator = handlers.GetEnumerator();
            Func<ActionHandler> getNextHandler = () =>
            {
                if (enumerator.MoveNext())
                {
                    return enumerator.Current;
                }
                return null;
            };
            ExcuteActionHandler(getNextHandler(), context, getNextHandler); 
        }

        internal void ExcuteActionHandler(ActionHandler handler, CMSActionContext context, Func<ActionHandler> getNext)
        {
            if (!context.Canceled && context.IsValid)
            {
                handler.Validate(context);
                var next = getNext();
                if (next != null)
                {
                    ExcuteActionHandler(next, context, getNext);
                }
                handler.Handle(context);
            }
        }
        
        #endregion

    }

    public class ActionHandler
    {
        public ActionHandler(string name)
        {
            Name = name;
        }

        public string Name { get; set; }

        public void Validate(CMSActionContext context)
        { 
        }

        public void Handle(CMSActionContext context)
        { 
        }
    }

    public class CMSActionContext
    {
        public string ActionName { get; set; }
        public bool Canceled { get; set; }
        public bool IsValid { get; set; }

        public CMSActionRequest Request { get; set; }
        public CMSActionResponse Response { get; set; }
    }

    public class CMSActionRequest
    { 
    }

    public class CMSActionResponse
    { 
    }
}